package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.Pack.*;
import com.deer.wms.base.system.service.PackDetailService;
import com.deer.wms.base.system.service.PackService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/11/18.
*/
@Api(description = "包装材料信息api接口")
@RestController
@RequestMapping("/packs")
public class PackController {

    @Autowired
    private PackService packService;

    @Autowired
    private PackDetailService packDetailService;


    @ApiOperation(value = "新增pack以及pack列表", notes = "新增pack以及pack列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody Pack pack , @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<Pack> list = packService.findByPackData(pack.getPackData());
        if(list.size() !=0){
            throw new ServiceException(CommonCode.PACK_DATA_EXISTS,"此包材名称已存在");
        }else{
            String nowDate = DateUtils.getNowDateTimeString();
            pack.setCreateTime(nowDate);
            packService.save(pack);
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/delete")
    @ApiOperation(value = "删除pack信息", notes = "删除pack信息")
    public Result delete(Integer id) {
        List<PackDetailDto> list = packDetailService.findByPackId(id);
        if(list.size() == 0){
            packService.removeById(id);
        }else {
            throw new ServiceException(CommonCode.PACK_DATA_EXISTS,"此包材名称正在使用");
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @PostMapping("/update")
    @ApiOperation(value = "更新pack信息", notes = "更新pack信息")
    public Result update(@RequestBody Pack pack) {
        Pack pack1 = packService.getById(pack.getId());
        if (pack1 != null) {
            packService.updateById(pack);
        }else {
            throw new ServiceException(CommonCode.PACK_DATA_EXISTS,"此包材不存在");
        }

        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/detail")
    public Result detail(@PathVariable Integer id) {
        Pack pack = packService.getById(id);
        return ResultGenerator.genSuccessResult(pack);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(PackCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<Pack> list = packService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
